package com.iflytek.testtoastwindow.utils;

import android.content.Context;
import android.view.WindowManager;

import java.lang.reflect.Field;

public class AppTools {
	/**
	 * 获取屏幕宽度
	 * @param conetxt
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static int getScreenWidth(Context conetxt)
	{
		WindowManager wm = (WindowManager) conetxt
                .getSystemService(Context.WINDOW_SERVICE);
		return wm.getDefaultDisplay().getWidth();
	}
	
	/**
	 * 获取屏幕高度
	 * @param context
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static int getScreenHeight(Context context)
	{
		WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
		return wm.getDefaultDisplay().getHeight();
	}

	/**
	 * 获取屏幕方向
	 * @param context
	 * @return {@link android.view.Surface#ROTATION_0} {@link android.view.Surface#ROTATION_90} {@link android.view.Surface#ROTATION_180} {@link android.view.Surface#ROTATION_270}
	 */
	public static int getScreenRotation(Context context)
	{
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		int rotation = wm.getDefaultDisplay().getRotation();
		return rotation;
	}


	/**
	 * 记录系统状态栏的高度
	 */
	private static int statusBarHeight;

	/**
	 * 获取系统栏高度
	 * 
	 * @return 返回状态栏高度的像素值
	 */
	public static int getStatusBarHeight(Context context) {
		if (statusBarHeight == 0) {
			try {
				Class<?> c = Class.forName("com.android.internal.R$dimen");
				Object o = c.newInstance();
				Field field = c.getField("status_bar_height");
				int x = (Integer) field.get(o);
				statusBarHeight = context.getResources().getDimensionPixelSize(
						x);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return statusBarHeight;
	}
}
